package com.skyhigh.web.webservice.exceptions;

import java.io.Serializable;

public class ExceptionHelper {
	
	public static void logEx(Exception ex){
		//TODO: Exception logging
	}
	
	public static Exception newEx(String message){
		//TODO: Exception logging
		return new Exception(message);
	}
	
	public static Exception newEx(Exception ex){
		//TODO: Exception logging
		return ex;
	}

	public static Exception newEx(String message, Object data){
		//TODO: Exception logging
		return new Exception(message);
	}
	
	public static Exception newEx(Exception ex, Object data){
		//TODO: Exception logging
		return ex;
	}	
	
	public static Exception newEx(Class exClass, String message){
		return newEx(exClass, message, null);		
	}
	
	public static Exception newEx(Class exClass, String message, Object data){
		//TODO: Exception logging
		Exception ex=null;
		if(exClass != null){			
			try {
				Object custom = exClass.newInstance();
				if(custom instanceof Exception){
					ex=(Exception)custom;						
				}				
			} catch (Exception e) {
				e.printStackTrace();				
			}
		}
		return ex == null ? new Exception() : ex;		
	}

	public static RuntimeException newRuntimeEx(String message){
		//TODO: Exception logging
		return new RuntimeException(message);
	}
	
	public static RuntimeException newRuntimeEx(Exception ex){
		//TODO: Exception logging
		if(ex instanceof RuntimeException){
			return (RuntimeException)ex;
		}else{
			return new RuntimeException(ex);
		}
	}

	public static RuntimeException newRuntimeEx(String message, Object data){
		//TODO: Exception logging
		return new RuntimeException(message);
	}
	
	public static RuntimeException newRuntimeEx(Exception ex, Object data){
		//TODO: Exception logging
		return new RuntimeException(ex);
	}	
	
	public static RuntimeException newRuntimeEx(Class exClass, String message){
		return newRuntimeEx(exClass, message, null);	
	}
	
	public static RuntimeException newRuntimeEx(Class exClass, String message, Object data){
		//TODO: Exception logging
		RuntimeException ex=null;
		if(exClass != null){			
			try {
				Object custom = exClass.newInstance();
				if(custom instanceof RuntimeException){
					ex=(RuntimeException)custom;						
				}				
			} catch (Exception e) {
				e.printStackTrace();				
			}
		}
		return ex == null ? new RuntimeException() : ex;		
	}	
	
	public static ServiceException newServiceEx(Exception ex){
		//TODO: Exception logging
		return new ServiceException(ex);
	}

	public static ServiceException newServiceEx(String message){
		//TODO: Exception logging
		return new ServiceException(message);
	}

	public static ServiceException newServiceEx(Exception ex, Object data){
		//TODO: Exception logging
		return new ServiceException(ex);
	}

	public static ServiceException newServiceEx(String message, Object data){
		//TODO: Exception logging
		return new ServiceException(message);
	}	

	public static ServiceException newPrimaryKeyNotFountEx(){
		//TODO: Exception logging
		return new PrimaryKeyNotFoundException();
	}	
	
	public static ServiceException newPrimaryKeyNotFountEx(Serializable key){
		//TODO: Exception logging
		return new PrimaryKeyNotFoundException(key);
	}	

	public static ValidationException newValidationEx(String message){
		//TODO: Exception logging
		return new ValidationException(message);
	}
	
	public static ValidationException newValidationEx(String... propertyNames){
		String message = "Required properties are not defined";
		String propList = "";
		for(String prop : propertyNames){
			propList += (" " + prop + " ");
		}
		//TODO: Exception logging
		return new ValidationException(message + " ("+propList+")");
	}	
	
	public static String catchEx(Exception cause){
		String message=cause.getMessage();
		//TODO: Exception logging		
		cause.printStackTrace();
		return message;
	}
	
	public static String catchEx(Exception cause, Object data){
		String message=cause.getMessage();
		//TODO: Exception logging		
		cause.printStackTrace();
		return message;
	}
	
	
	
	
	
	@Deprecated
	public static ServiceException serviceException(Object origin, String message){
		//TODO: Exception logging		
		return new ServiceException(message);
	}
	
	@Deprecated
	public static ServiceException serviceException(Object origin, Exception cause){
		//TODO: Exception logging		
		return serviceException(origin, cause, null);
	}
	
	@Deprecated
	public static ServiceException serviceException(Object origin, Exception cause, Object data){
		//TODO: Exception logging		
		if(cause instanceof ServiceException){
			return (ServiceException)cause;
		}
		return new ServiceException(cause);		
		
	}	

	@Deprecated
	public static String catchException(Object origin, Exception cause){
		String message=cause.getMessage();
		//TODO: Exception logging		
		cause.printStackTrace();
		return message;
	}
	
	@Deprecated
	public static String catchException(Object origin, Exception cause, Object data){
		String message=cause.getMessage();
		//TODO: Exception logging		
		cause.printStackTrace();
		return message;
	}

}
